package net.zoneland.data.dataprofiling.system.service.impl;

import net.zoneland.data.dataprofiling.common.domain.Tree;
import net.zoneland.data.dataprofiling.common.utils.BuildTree;
import net.zoneland.data.dataprofiling.system.domain.DeptEntity;
import net.zoneland.data.dataprofiling.system.repository.DeptRepository;
import net.zoneland.data.dataprofiling.system.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeptServiceImpl implements DeptService {


    @Autowired
    private DeptRepository deptRepository;

    @Override
    public DeptEntity get(Long deptId) {
        return deptRepository.getOne(deptId);
    }

    @Override
    public List<DeptEntity> list() {
        Sort sort = Sort.by(new Sort.Order(Sort.Direction.ASC,"orderNum"));
        return deptRepository.findAll(sort);
        //return null;
    }

    @Override
    public int count() {
        return (int) deptRepository.count();
    }

    @Override
    public int save(DeptEntity sysDept) {
        DeptEntity deptEntity = deptRepository.save(sysDept);
        if(deptEntity!=null){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public int update(DeptEntity sysDept) {
        DeptEntity deptEntity = deptRepository.saveAndFlush(sysDept);
        if(deptEntity!=null){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public int remove(Long deptId) {
        deptRepository.deleteById(deptId);
        return 1;
    }

    @Override
    public int batchRemove(Long[] deptIds) {
        for (Long id:deptIds){
            remove(id);
        }
        return deptIds.length;
    }

    @Override
    public Tree<DeptEntity> getTree() {
        List<Tree<DeptEntity>> trees = new ArrayList<Tree<DeptEntity>>();
        List<DeptEntity> sysDepts = this.list();
        for (DeptEntity sysDept : sysDepts) {
            Tree<DeptEntity> tree = new Tree<DeptEntity>();
            tree.setId(sysDept.getDeptId().toString());
            tree.setParentId(sysDept.getParentDept().getDeptId().toString());
            tree.setText(sysDept.getName());
            Map<String, Object> state = new HashMap<>(16);
            state.put("opened", true);
            tree.setState(state);
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        Tree<DeptEntity> t = BuildTree.build(trees);
        return t;
    }

    @Override
    public boolean checkDeptHasUser(Long deptId) {
        DeptEntity deptEntity = deptRepository.getOne(deptId);
        if(deptEntity.getUsers()!=null && deptEntity.getUsers().size()>0){
            return true;
        }else{
            return false;
        }

    }

    @Override
    public List<Long> listChildrenIds(Long parentId) {
        List<DeptEntity> deptEntities = list();
        return treeMenuList(deptEntities,parentId);
    }

    List<Long> treeMenuList(List<DeptEntity> menuList, long pid) {
        List<Long> childIds = new ArrayList<>();
        for (DeptEntity mu : menuList) {
            //遍历出父id等于参数的id，add进子节点集合
            if (mu.getParentDept().getDeptId() == pid) {
                //递归遍历下一级
                treeMenuList(menuList, mu.getDeptId());
                childIds.add(mu.getDeptId());
            }
        }
        return childIds;
    }
}
